#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "common.h"
#include "yuan_mqtt.h"
#include "yuan_pthread.h"
#include "shm.h"



int main(int argc, char* argv[]) {

     pthread_t t1, t2, t3;
 // 1. 初始化共享内存
    shm_addr = shm_init();
    if (!shm_addr) {
        printf("[元宇宙] 共享内存初始化失败，退出\n");
        return -1;
    }
    printf("[元宇宙] 共享内存初始化成功(键值:0x%x,大小：%d)\n", SHM_KEY, SHM_SIZE);

    
    if (create_yuan_threads(&t1, &t2, &t3) != 0) {
        yuan_quit = 1;
        shm_destory(shm_addr);
        printf("[main] 线程创建失败，程序退出\n");
        return 0;
    }

    // 3. 等待用户退出
    printf("[元宇宙] 系统启动完成,按Q退出...\n");
    char ch;
    do {
        ch = getchar();
    } while (ch != 'Q' && ch != 'q' && !yuan_quit);

    // 4. 退出清理
    yuan_quit = 1;
    printf("[元宇宙] 开始清理资源...\n");

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    pthread_join(t3, NULL);


    shm_destory(shm_addr);
    printf("连接进程退出\n");
    return 0;

}


